﻿using Catalogo.Portable.DataSources;
using Catalogo.Portable.ViewModel;
using Catalogo.Windows8.EnableLiveTile.NotificationsExtensions.TileContent;
using Catalogo.Windows8.EnableLiveTile.TileContent;
using System.Threading.Tasks;
using Windows.UI.Notifications;

namespace Catalogo.Windows8.EnableLiveTile
{
    public class CreateLiveTile
    {
        public static async Task ShowLiveTileAsync()
        {
            var empresa = await CatalogoDataSource.Instance.ObterEmpresaAsync();

            var tileUpdater = TileUpdateManager.CreateTileUpdaterForApplication();

            CreateTileUpdater(tileUpdater, empresa.Nome, empresa.LiveTileBig1, empresa.LiveTileSmall1);
        }

        private static void CreateTileUpdater(TileUpdater tileUpdater, string nome, string wideTile, string smallTile)
        {
            var _baseUri = "ms-appx:///";

            var wideTilesrc = string.Format("{0}{1}", _baseUri, wideTile);
            var smallTilesrc = string.Format("{0}{1}", _baseUri, smallTile);

            var tileContent = TileContentFactory.CreateTileWideImageAndText01();
            tileContent.TextCaptionWrap.Text = nome;
            tileContent.Image.Src = wideTilesrc;
            tileContent.Image.Alt = nome;

            var squareContent = TileContentFactory.CreateTileSquareImage();
            squareContent.Image.Src = smallTilesrc;
            squareContent.Image.Alt = nome;
            tileContent.SquareContent = squareContent;

            tileUpdater.Update(tileContent.CreateNotification());
        }

    }
}
